package com.hqyj.xxl.service.impl;

import com.hqyj.xxl.Dao.RoleDao;
import com.hqyj.xxl.Dao.RolePermissionDao;
import com.hqyj.xxl.pojo.Permission;
import com.hqyj.xxl.pojo.Role;
import com.hqyj.xxl.pojo.RolePermission;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import javax.xml.validation.SchemaFactory;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Set;

import static org.junit.Assert.*;

public class RoleServiceImplTest {
    private  RoleServiceImpl roleService;
    private SqlSession sqlSession;
    @Before
    public void setUp() throws Exception {
        //通过SqlSessionFactoryBuilder读取mybatis基础配置文件来构建sqlSessionFactory
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        try (InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml")){
            SqlSessionFactory sqlSessionFactory =builder.build(inputStream);
            sqlSession = sqlSessionFactory.openSession(true);
            RoleDao roleDao= sqlSession.getMapper(RoleDao.class);
            RolePermissionDao rolePermissionDao = sqlSession.getMapper(RolePermissionDao.class);
            roleService = new RoleServiceImpl(roleDao,rolePermissionDao);
        }catch (IOException e){
            e.printStackTrace();
        }
    }

    @Test
    public void getAll() {
        Set<Role> roleSet = roleService.getAll();
        System.out.println(roleSet);
    }

    @Test
    public void addOne() {
        Role role =new Role();
        role.setName("管理员");
        role.setState(0);
        Set<Permission> permissions =new HashSet<>();
        Permission p1 = new Permission();
        Permission p2 = new Permission();
        p1.setPermissionId(9);
        p2.setPermissionId(8);
        permissions.add(p1);
        permissions.add(p2);
        role.setPermissions(permissions);
        roleService.addOne(role);
    }

    @Test
    public void removeOne() {
    }

    @Test
    public void modifyOne() {
    }
    @Test
    public void removeSome(){
        Set<Integer> roleIds = new HashSet<>();
        roleIds.add(3);
        roleIds.add(4);
        roleIds.add(5);
        roleIds.add(6);
        roleIds.add(7);
        roleService.removeSome(roleIds);
    }
}